package pf.springBoot.springBootSSO.oAuth;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.perfect99.right.amsweb.ActionValidate;
import com.perfect99.right.amsweb.ActionValidateSoap;
import pf.springBoot.springBootSSO.oAuth.model.UserAllInfoDto;

@Component
public class MetabaseFormsAuth {

//	 private static PFCookieUtils _cookieUtils;
    private static  ActionValidateSoap _service;
    //private static String cookieKey="cookieKey";//相当于FormsAuthentication.FormsCookieName


    private static String _appMapCode = "Metabase";
//
	@Autowired
	public MetabaseFormsAuth(//PFCookieUtils cookieUtils
			)
    {
		
		//_cookieUtils=cookieUtils;

    	ActionValidate factory = new ActionValidate();
        //根据工厂创建一个WeatherWSSoap对象
    	ActionValidateSoap weatherWSSoap = factory.getActionValidateSoap();
    	 _service= weatherWSSoap;
//    	BaseReturnInfo returnInfo = PFDataHelper.UseLocalData()
//          ? null// JsonConvert.DeserializeObject<BaseReturnInfo>(PFDataHelper.ReadLocalJson("LoginCheck.json")) //本地方法待写-- benjamin todo
//          : _service.loginValidate("1712002", "1712002");
////        _service = new ActionValidateServiceReference.ActionValidateSoapClient();
    }

    public static MetabaseUser GetMetabaseUser(String userid)
    {
        if(userid.length()>=9&& userid.startsWith("101"))
        {
            userid = userid.substring(3);
        }
//    	com.perfect99.right.amsweb.UserAllInfo userAllInfo = FormsAuth.GetUserInfo(userid);
    	UserAllInfoDto userAllInfo = FormsAuth.GetUserInfoWebApi(userid);
        //var userAllInfo = FormsAuth.GetUserInfoByWeb(userid);
        if (userAllInfo == null)
        {
            return null;
        }
        MetabaseUser metabaseUser = null;
        com.perfect99.right.amsweb.RoleReturnInfo metabaseAuth = MetabaseFormsAuth.GetRoles(userid);
        if ( "电脑部".equals(userAllInfo.getOrgName()))//OrgCode 101.06 metabase登陆密码5ZE3xuJDJqWLD9
        {
            metabaseUser = new MetabaseUser
            (
                 "ComputerDepartment",
                 "电脑部",
                 "computerdepartment@perfect99.com"
            );
        }
        else if (metabaseAuth.getRoles() != null && metabaseAuth.getRoles().getRoleInfo().size() > 0)
        {
        	String roleCode = metabaseAuth.getRoles().getRoleInfo().get(0).getNumber().replace("Metabase_", "");
            metabaseUser = new MetabaseUser
            (
                 roleCode,
                 metabaseAuth.getRoles().getRoleInfo().get(0).getName(),
                 roleCode.toLowerCase()+ "@perfect99.com"
            );
        }
        else
        {
            metabaseUser = new MetabaseUser//全部用户 3pcbSDL_S?t5Kv
            (
                 "AllUser",
                 "(全部用户)",
                 "alluser@perfect99.com"
            );

        }
        return metabaseUser;
    }

    private static com.perfect99.right.amsweb.RoleReturnInfo GetRoles(String userid)
    {
        //return _webService.GetRoles(userid, _appMapCode, null, null, null, null, null, null);
        return  _service.getRoles(userid, _appMapCode, null, null, null, null, null, null);
    } 
	
	
}
